﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace KetBanBonPhuong
{
    /// <summary>
    /// Summary description for Member
    /// </summary>
    public class Member
    {
        private int member_id;

        public int Member_id
        {
            get { return member_id; }
            set { member_id = value; }
        }

        private int member_list_id;

        public int Member_list_id
        {
            get { return member_list_id; }
            set { member_list_id = value; }
        }

        private int member_user_id;

        public int Member_user_id
        {
            get { return member_user_id; }
            set { member_user_id = value; }
        }

        private bool is_subscriber;

        public bool Is_subscriber
        {
            get { return is_subscriber; }
            set { is_subscriber = value; }
        }

        private DateTime created_date;

        public DateTime Created_date
        {
            get { return created_date; }
            set { created_date = value; }
        }

        public Member()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Member(SqlDataReader sdr)
        {
            if (sdr != null && !sdr.IsClosed)
            {
                member_id = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    member_list_id = sdr.GetInt32(1);
                if (!sdr.IsDBNull(2))
                    member_user_id = sdr.GetInt32(2);
                if (!sdr.IsDBNull(3))
                    is_subscriber = sdr.GetBoolean(3);
                if (!sdr.IsDBNull(4))
                    created_date = sdr.GetDateTime(4);
            }
        }

        public static DataTable GetAll()
        {
            DBHelp dbh = new DBHelp();
            DataTable dt = new DataTable();
            string cmd = "Member_GetAll";
            dt = dbh.ExecuteDataTable(cmd, CommandType.StoredProcedure);
            return dt;
        }

        public static DataTable GetAll(int MemListID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@member_list_id", MemListID);
            String cmd = @"select * from tbl_member where member_user_id = @member_list_id";
            DataTable dt = dbh.ExecuteDataTable(cmd, CommandType.Text);
            return dt;
        }

        public static int GetMemberID(int UserID, int GroupID)
        {
            int MemberListID = MemberList.GetMemberListID(GroupID);
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@member_user_id", UserID);
            dbh.AddParameter("@member_list_id", MemberListID);
            String cmd = @"select * from tbl_member where member_user_id = @member_list_id and @member_list_id = member_list_id";
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.Text);
            if (sdr.Read())
            {
                return sdr.GetInt32(0);
            }
            else
            {
                return -1;
            }
        }

        //Ham lay ve ID nhan vao
        public static Member GetInfo(int ID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@member_id", ID);
            Member member = new Member();
            string cmd = "Member_GetInfo";
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            if (sdr.Read())
            {
                member.Member_id = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    member.Member_list_id = sdr.GetInt32(1);
                if (!sdr.IsDBNull(2))
                    member.Member_user_id = sdr.GetInt32(2);
                if (!sdr.IsDBNull(3))
                    member.Is_subscriber = sdr.GetBoolean(3);
                if (!sdr.IsDBNull(4))
                    member.Created_date = sdr.GetDateTime(4);
            }
            return member;
        }

        //Them member
        public static void Insert(Member member)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@member_list_id", member.Member_list_id);
            dbh.AddParameter("@member_user_id", member.Member_user_id);
            dbh.AddParameter("@is_subscriber", member.Is_subscriber);
            dbh.AddParameter("@created_date", member.Created_date);
            string cmd = "Member_Insert";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        //Sua member
        public static void Update(Member member)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@member_id", member.Member_id);
            dbh.AddParameter("@member_list_id", member.Member_list_id);
            dbh.AddParameter("@member_user_id", member.Member_user_id);
            dbh.AddParameter("@is_subscriber", member.Is_subscriber);
            dbh.AddParameter("@created_date", member.Created_date);
            string cmd = "Member_Update";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        //Xoa member
        public static void Delete(int ID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@member_id", ID);
            string cmd = "Member_Delete";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        // can thiet them sau
    }
}